package cn.vawn.exam.service.impl;

import cn.vawn.base.dto.PageDTO;
import cn.vawn.base.dto.RequestPageDTO;
import cn.vawn.exam.entity.LocalUniterm;
import cn.vawn.exam.mapper.LocalUnitermMapper;
import cn.vawn.exam.service.ILocalUnitermService;
import cn.vawn.exam.service.exception.DataNotFoundException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

//import cn.vawn.exam.constant.MedicalCard;

@Service
public class LocalUnitermServiceImpl implements ILocalUnitermService {

	@Resource
	private LocalUnitermMapper localUnitermMapper;
	
	public PageDTO getAllLocalUniterm(RequestPageDTO requestPageDTO) {
		PageDTO pd = new PageDTO();

		int pageNumber = requestPageDTO.getPageNumber();
		int pageSize = requestPageDTO.getPageSize();
		String sort = requestPageDTO.getSort();
		String desc = requestPageDTO.getDesc();
		if(pageNumber > 0 &&  pageSize > 0){
			PageHelper.startPage(pageNumber, pageSize);
		}
		if(!sort.isEmpty() && !desc.isEmpty()){
			PageHelper.orderBy(sort + " " + desc);
		}

		List<LocalUniterm> uniterms =
				localUnitermMapper.selectAllLocalUniterm();

		pd.setPageNumber(pageNumber);
		pd.setPageSize(pageSize);
		pd.setTotal((int) new PageInfo(uniterms).getTotal());
		pd.setData(new PageInfo(uniterms).getList());

		return pd;
	}

	public PageDTO getPartLocalUniterm(RequestPageDTO requestPageDTO,
									   String localUnitermName, String code, Date startTime, Date endTime) {
		
//		List<LocalUniterm> partLocalUniterms = localUnitermMapper.selectLocalUniterm(localUnitermName, code, startTime, endTime);

		PageDTO pd = new PageDTO();

		int pageNumber = requestPageDTO.getPageNumber();
		int pageSize = requestPageDTO.getPageSize();
		String sort = requestPageDTO.getSort();
		String desc = requestPageDTO.getDesc();
		if(pageNumber > 0 &&  pageSize > 0){
			PageHelper.startPage(pageNumber, pageSize);
		}
		if(!sort.isEmpty() && !desc.isEmpty()){
			PageHelper.orderBy(sort + " " + desc);
		}
		List<LocalUniterm> uniterms =
				localUnitermMapper.selectLocalUniterm(localUnitermName, code, startTime, endTime);
//		for(LocalUniterm localUniterm : uniterms){
//			System.out.println(localUniterm);
//		}
		pd.setPageNumber(pageNumber);
		pd.setPageSize(pageSize);
		pd.setTotal((int) new PageInfo(uniterms).getTotal());
		pd.setData(new PageInfo(uniterms).getList());

		return pd;
	}

	@Transactional
	public void dltLocalUnitermByid(int localUnitermId) {
		//
		LocalUniterm localUniterm = localUnitermMapper.selectByPrimaryKey(localUnitermId);
		
		//
		if(localUniterm == null) {
			//
			throw new DataNotFoundException("数据不存在，请刷新后重试");
		}
		
		localUnitermMapper.deleteByPrimaryKey(localUnitermId);
	}

	public void updateLocalUnitermById(LocalUniterm localUniterm) {
		//
		LocalUniterm uniterm = localUnitermMapper.selectByPrimaryKey(localUniterm.getId());
		//
		if(uniterm == null) {
			//
			throw new DataNotFoundException("数据不存在，请刷新后重试");
		}
		
		localUnitermMapper.updateByPrimaryKey(localUniterm);
	}

	public int addLocalUniterm(LocalUniterm localUniterm) {
		
//		LocalUniterm uniterm = localUnitermMapper.selectLocalUnitermById(localUniterm.getLocalUnitermId());
//		if(uniterm != null) {
//			throw new DataNotFoundException("数据不存在，请刷新后重试");
//		}		
		
		localUniterm.setCreatedTime(new Date());
		int line = localUnitermMapper.insert(localUniterm);
//		if(line == MedicalCard.RETURN_NUM_ADD_FALSE) {
//			return line;
//		}
		
		return line;
	}

	public LocalUniterm getLocalUniterm(int localUnitermId) {
		LocalUniterm localUniterm = localUnitermMapper.selectByPrimaryKey(localUnitermId);
		if(localUniterm == null) {
			//数据不存在
			throw new DataNotFoundException("数据不存在，请刷新后重试");
		}
		return localUniterm;
	}


	@Override
	public List<LocalUniterm> getLocalUnitermByIds(Integer[] localUnitermIds) {
		return localUnitermMapper.getLocalUnitermByIds(localUnitermIds);
	}
}
